module game
{
	export class Login extends eui.Component
	{
		private input:eui.TextInput;
		private confirmBtn:eui.Button;
		public constructor()
		{
			super();
			this.skinName = "resource/game_ui/Login.exml";
			this.addEventListener(egret.Event.COMPLETE,this.loaded,this);
		}

		protected loaded(): void
		{
			this.removeEventListener(egret.Event.COMPLETE,this.loaded,this);
			this.confirmBtn.addEventListener(egret.TouchEvent.TOUCH_TAP,this.onBtn,this);
			this.input.addEventListener(egret.Event.CHANGE,this.textChange,this);
			this.confirmBtn.visible = false;
		}

		private onBtn():void
		{
			this.confirmBtn.enabled = false;
			this.confirmBtn.removeEventListener(egret.TouchEvent.TOUCH_TAP,this.onBtn,this);
			this.input.removeEventListener(egret.Event.CHANGE,this.textChange,this);
			GameManager.getInstance().login(this.input.text,this.loginOK,this);
		}

		private loginOK():void
		{
			let maps = GlobalData.accData.maps;
			if(maps && maps.length > 0)
			{
				GameManager.getInstance().getMapByAcc(GlobalData.accData.acc,this.mapReturn,this);
			}
			else
			{
				UIManager.getInstance().openEditor();
			}
		}

		private mapReturn():void
		{
			game.UIManager.getInstance().openEditor(2);
		}

		private textChange():void
		{
			this.confirmBtn.visible = this.input.text.length > 0;
		}
	}
}